package com.fms.gui.templateCreator.components.date.parts;

public class TimeAngleCalculator
{
	public TimeAngleCalculator()
	{
		
	}

	public double angleForHour(int hour, int minute)
	{
		double totalMinutes = hour * 60 + minute;		
		return (totalMinutes / 720) * (2 * Math.PI);
	}

	public double hourFromVector(final double[] position)
	{
		double[] reference = new double[] {0.0, 1.0};	


		double angle = getAngleFromVectors(position, reference);
		
		return angle;
	}
	
	
	public double getAngleFromVectors(double[] a, double[] b)
	{
		double aTotal = Math.sqrt(Math.pow(a[0], 2.0) 
				   + Math.pow(a[1], 2.0));
		
		double bTotal = Math.sqrt(Math.pow(b[0], 2.0) 
				   	  + Math.pow(b[1], 2.0));

		double[] normA = new double[]{a[0]/ aTotal,
									  a[1]/ aTotal	};
		
		double[] normB = new double[]{ b[0]/ bTotal,
				 					   b[1]/ bTotal	};

		
		double dot = normA[0] * normB[0] + 
					 normA[1] * normB[1];
		
		double angle = Math.acos(dot);
		
		return angle;
	}

	public double getAngleFromNorth(double[] newPosition)
	{
		
		double angle = getAngleFromVectors(newPosition, new double[] {0.0, 1.0});
		if(newPosition[0] < 0)
		{
			angle = (Math.PI * 2) - angle;
		}
		return angle;
	}

	public int getMinute(double[] newPosition)
	{
		
		double angle = getAngleFromNorth(newPosition);

		
		double percentage = angle/(Math.PI * 2);
		
		return (int)(percentage * 60);
	}

	public double minuteToAngle(int minute)
	{
		double percentage = (double)minute / 60.0;
		
		
		return (Math.PI * 2) * percentage;
	}
	
}
